<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket Client</title>
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="index.css">
    <script src="node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
    <div id="body">
        <div class="form-group">
            <label for="serverIP">服务器IP</label>
            <input type="text" class="form-control" id="serverIP" v-model="server_ip" placeholder="服务器IP">
        </div>
        <div class="form-group">
            <label for="serverPort">服务器端口号</label>
            <input type="number" class="form-control" id="serverPort" v-model="server_port" min="0" step="1" max="65535" placeholder="服务器端口号">
        </div>
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" class="form-control" id="username" v-model="username" placeholder="用户名">
        </div>
        <button type="button" class="btn btn-primary" style="width: 100%;" v-on:click="save_config">保存</button>
    </div>
    <div class="navbar">
        <a href="index.html">聊天</a>
        <a href="settings.html">设置</a>
    </div>
</div>
<script type="text/javascript">
    const server_ip = window.android.get_pref('server_ip');
    const port = window.android.get_pref('port');
    const cur_username = window.android.get_pref('username');
    new Vue({
        el: '#app',
        data: {
            server_ip: server_ip,
            server_port: port,
            username: cur_username,
        },
        methods: {
            save_config () {
                if (this.username.indexOf(',') !== -1 ||
                    this.username.indexOf('\'') !== -1 ||
                    this.username.indexOf('"') !== -1) {
                    window.android.show_toast_long('用户名中不能包含英文逗号与单双引号');
                } else if (this.server_ip === '' ||
                    this.server_port === '' ||
                    this.username === '') {
                    window.android.show_toast_long('表单不能有空');
                } else {
                    window.android.save_config(this.server_ip, this.server_port, this.username);
                    window.android.show_toast_long('保存成功');
                }
            }
        }
    });
</script>
</body>
</html>
